﻿using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using XF.Common;
using XF.Common.Redis;
using XF.Common.WeChat;
using XF.Meeting.DAL;
using XF.Meeting.Entity.Query;
using XF.Meeting.Entity.View;

namespace QuartzJob.Jobs
{
    public class SendWxMsg : IJob
    {
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                var dal = new MeetingDal();
                var aa = dal.GetUserListForWX(DateTime.Now, out string StrError);
                if (StrError == "")
                {
                    var list1 = TableToList.ToList<meetIdS>(aa.Result);
                    if (list1.Count > 0)
                    {
                        foreach (var item in list1)
                        {
                            SendWxMssage(item.ID, true);
                        }
                        return Task.CompletedTask;
                    }
                    else
                    {
                        return Task.CompletedTask;
                    }
                }
                return Task.CompletedTask;
            }
            catch (Exception e)
            {

                return Task.CompletedTask;
            }

        }
        public static void SendWxMssage(int ID, bool isTen = false)
        {
            var dal = new MeetingDal();
            var res = dal.GetCInfoByID(ID).Result;

            var list = TableToList.ToList<XF.Meeting.Entity.View.MeetingInfoSingle>(res);
            var meetingInfoSingle = list[0];
            meetingInfoSingle.date = Convert.ToDateTime(meetingInfoSingle.BeginTime).ToString("yyyy-MM-dd");
            meetingInfoSingle.Begin = Convert.ToDateTime(meetingInfoSingle.BeginTime).ToString("HH:mm");
            meetingInfoSingle.End = Convert.ToDateTime(meetingInfoSingle.EndTime).ToString("HH:mm");
            if (string.IsNullOrEmpty(meetingInfoSingle.CRooms))
            {
                meetingInfoSingle.CRooms = meetingInfoSingle.CRoom;
            }
            else
            {
                meetingInfoSingle.CRooms = meetingInfoSingle.CRoom +"/" +meetingInfoSingle.CRooms;
                meetingInfoSingle.CRooms = meetingInfoSingle.CRooms.Substring(0, meetingInfoSingle.CRooms.Length - 1);
            }
         //  meetingInfoSingle.CRooms = meetingInfoSingle.CRoom + "," + meetingInfoSingle.CRooms;
            var msgList = dal.getMeetInfoAndUserPhone(ID, out string StrError);
            var phoneList = TableToList.ToList<meetIdS>(msgList.Tables[0]);
            var phone = phoneList.Select(i => i.Account).ToList();
            var userr = GetUserString(phone);
         
            var msg = new Message(new Text($"{meetingInfoSingle.CoordinatorName}邀请您参加《{meetingInfoSingle.ConferenceTheme}》会议，时间 {meetingInfoSingle.date}  {meetingInfoSingle.Begin}~{meetingInfoSingle.End}，地点：{meetingInfoSingle.CRooms}，主持人：{meetingInfoSingle.HostPersonName}，请准时出席。"), userr);
          
            switch (meetingInfoSingle.wxType)
            {
                case 1:
                    var wx1 = new  WeChatHelper();
                    wx1.SendTextMessage(msg);
                    wx1.SendTPwdToCreator(new CRoomPadQuery() { CInfoId = ID, type = 1 }, false);
                    break;
                case 2:
                    var wx2 = new WyWeChatHelper();
                    wx2.SendTextMessage(msg);
                    wx2.SendTPwdToCreator(new CRoomPadQuery() { CInfoId = ID, type = 1 }, false);
                    break;
                default:
                    var wx3 = new WeChatHelper();
                    wx3.SendTextMessage(msg);
                    wx3.SendTPwdToCreator(new CRoomPadQuery() { CInfoId = ID, type = 1 }, false);
                    break;
            }
            if (isTen)
            {
                dal.updateWxState(ID, out string strError);
            }


        }
        public static string GetUserString(List<string> userList)
        {
            StringBuilder sb = new StringBuilder();

            foreach (var item in userList)
            {
                //if (string.IsNullOrWhiteSpace(RedisCache.Hash_Get<string>("UserPhone", item)))
                //{
                //    continue;
                //}

                sb.Append(item);
                sb.Append("|");
            }
            sb.Remove(sb.Length - 1, 1);
            return sb.ToString();
        }
        public class meetIdS
        {
            public int ID { get; set; }
            public string Phone { get; set; }
            public string Account { get; set; }
        }

    }

}
